export const excel = function excel(dom, data, column, fName = 'excel表格', title) {
  // console.log(dom)
  // clearTimeout(exTime)
  // // console.log(inExcel);
  // if (!inExcel) {
  //   inExcel = true
  // console.log('title', data)
  // console.log('item', column)
  let color = false
  let width = 750
  if (data.reduce) {
    width = data.reduce((pre, next) => {
      next.width = next.width ? next.width : 0
      return pre += next.width
    }, 0)
  }
  let str = ''
  if (title) {
    str += `<h1 style='text-align:center'>${title.companyName}</h1>` // 公司名称
    str += `<h3 style='text-align:center'>${title.title}</h3>` // 项目标题
    // console.log(title);
  }
  // 导出时间
  const begin = localStorage.getItem('printBeginTime')
  const end = localStorage.getItem('printEndTime')
  if (begin && end) {
    str += `<h5>导出时间:${new Date().format('yyyy-MM-dd HH:mm:ss')}</h5>`
    str += `<h5>起始时间：${begin}  结束时间：${end}</h5>`
  }
  str += `<table style='width:${width}px' border='1' margin-bottom:10px>`
  str += "<tr style='height:30px'>"
  const newColumn = []
  for (const i in data) {
    if (data[i].title !== '' && data[i].title !== '操作') {
      newColumn.push(data[i])
      str += `<th style='border:1px solid #ccc;' bgcolor='#fce4d6'>` + data[i].title + '</th>'
    }
  }
  str += '</tr><br/>'
  for (let i = 0; i < column.length; i++) {
    if (column[i].menuName == '合计' || column[i].hasRepaid || column[i].title === '合计') {
      str += `<tr style='height:30px;background-color:#ccc;'>`
    } else {
      str += `<tr ${column[i].bright ? "bgcolor='#d9e1f2'" : ''} style='height:30px'>`
    }

    for (let k = 0; k < newColumn.length; k++) {
      if (column[i][newColumn[k].key] == undefined) {
        column[i][newColumn[k].key] = ''
      }
      // // console.log(typeof String(column[i][data[k].key]))String("\""+column[i][data[k].key]+"\"")
      const tableText = ''
      // // console.log(column[i]);
      // // console.log(newColumn[k]);
      if ([newColumn[k].key] == 'cardNo') {
        tableText = '&nbsp;' + String(column[i][newColumn[k].key])
      } else {
        tableText = String(column[i][newColumn[k].key])
      }
      if (tableText.includes('合计') && typeof (tableText) !== Number) {
        color = true
      } else {
        color = false
      }
      //  else if (!tableText.includes('合计') && typeof (Number(tableText)) !== 'number' && tableText !== '') {
      //   color = false
      // } else if (!tableText.includes('合计') && typeof (Number(tableText)) !== 'number' && typeof (tableText) == 'string') {
      //   color = false
      // }
      // // console.log(tableText);
      // // console.log(typeof (Number(tableText)));
      // // console.log(color);
      str += `<td x:str style="width:${newColumn[k].width ? newColumn[k].width : 100}px;background:${color ? 'CornflowerBlue' : ''};text-align:center;border:1px solid #ccc;">${tableText.indexOf('0') == '0' ? '&nbsp;' : ''}${String(tableText).length >= '12' ? '&nbsp;' : ''}` + (tableText == 'true' || tableText == 'false' ? (tableText == 'true' ? '是' : '否') : tableText) + '</td>'
    }
    str += '</tr><br/>'
  }
  str += '</table>'
  str += '<br/>'
  // // console.log(str);
  // document.write(str);
  const type = 'excel'
  var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:" + type + "' xmlns='http://www.w3.org/TR/REC-html40'>"
  excelFile += '<head>'
  excelFile += '<meta http-equiv=Content-Type; content=text/html;charset=UTF-8>'
  excelFile += '<!--[if gte mso 9]>'
  excelFile += '<xml>'
  excelFile += '<x:ExcelWorkbook>'
  excelFile += '<x:ExcelWorksheets>'
  excelFile += '<x:ExcelWorksheet>'
  excelFile += '<x:Name>'
  excelFile += '{worksheet}'
  excelFile += '</x:Name>'
  excelFile += '<x:WorksheetOptions>'
  excelFile += '<x:DisplayGridlines/>'
  excelFile += '</x:WorksheetOptions>'
  excelFile += '</x:ExcelWorksheet>'
  excelFile += '</x:ExcelWorksheets>'
  excelFile += '</x:ExcelWorkbook>'
  excelFile += '</xml>'
  excelFile += '<![endif]-->'
  excelFile += '</head>'
  excelFile += '<body>'
  excelFile += str
  excelFile += '</body>'
  excelFile += '</html>'
  var base64data = 'base64,' + window.btoa(unescape(encodeURIComponent(excelFile)))
  switch (type) {
    case 'excel':
      // CSDN https://blog.csdn.net/xiewenfeng520/article/details/104563113?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_utm_term-4&spm=1001.2101.3001.4242
      // window.URL = window.URL || window.webkitURL
      // var binaryData = []
      // var BlobUrl = ''
      // binaryData.push(excelFile)
      // BlobUrl = window.URL.createObjectURL(new Blob(binaryData, {
      //   type: 'data:application/vnd.ms-' + type + ';'
      // }))
      // dom.href = BlobUrl
      // dom.download = fName + '.xls'
      // 原来的方法
      dom.href = 'data:application/vnd.ms-' + type + ';' + base64data // 必须是a标签，否则无法下载改名
      dom.download = fName + '.xls'
      break
  }
}